
package com.epam.android.training.day8;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity to demonstrate asyncTask Shows progress bar. There is a button under
 * it to interrupt process. Progress bar shows correctly even if phone has been
 * rotated.
 * 
 * @author Natallia_Kukharchyk
 */
public class AsyncTaskDemo extends Activity {

  private ProgressBar progressBar;

  private Button cancelButton;

  private MyAsyncTask task;

  private TextView status;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    progressBar = (ProgressBar) findViewById(R.id.progress_bar);
    progressBar.setMax(100);
    status = (TextView) findViewById(R.id.msg_view);
    cancelButton = (Button) findViewById(R.id.button1);
    cancelButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        task.cancel(true);
      }
    });
    // initialize with progress value - for case, when phone was rotated, just
    // to save current progress data
    task = new MyAsyncTask(getProgress());
    task.execute(getApplicationContext());

  }

  private int getProgress() {
    final Object data = getLastNonConfigurationInstance();
    // The activity is starting for the first time, load the data...
    if (data != null) {
      return ((MyAsyncTask) data).progress;
    } else {
      return 0;
    }

  }

  @Override
  public Object onRetainNonConfigurationInstance() {
    return task;
  }

  /**
   * Async Task (actually do nothing, but increase progress in some time
   * 
   * @author Natallia_Kukharchyk
   */
  class MyAsyncTask extends AsyncTask<Context, Integer, String> {

    MyAsyncTask(int progress) {
      this.progress = progress;
    }

    private int progress;

    @Override
    protected String doInBackground(Context... params) {

      int count = 100;
      int i = progress;
      while (i <= count) {
        try {
          Thread.sleep(300);
          progress = (i);
          publishProgress(progress);
        } catch (InterruptedException e) {
          e.printStackTrace();
          // This return causes onPostExecute call on UI thread
          return getString(R.string.interrupted);
        }
        i++;
      }
      return getString(R.string.finished);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      progressBar.setProgress(values[0]);

    }

    @Override
    protected void onCancelled() {
      super.onCancelled();
      status.setText(getString(R.string.progress) + getString(R.string.interrupted));
      Toast.makeText(getApplicationContext(), getString(R.string.interrupted), Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPostExecute(String result) {
      super.onPostExecute(result);
      status.setText(getString(R.string.progress) + getString(R.string.finished));
      cancelButton.setVisibility(View.INVISIBLE);
    }

  }

}
